package net.adetech.crj.controller;


import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.utils.InnerServiceUtil;
import net.adetech.framework.entity.ServiceRequest;
import net.adetech.framework.entity.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class SubmitTableController {


    @Autowired
    InnerServiceUtil innerServiceUtil;


    @RequestMapping(value = "/crj/submitTable", produces = "application/json")
    public String submitTable(HttpServletRequest request) {


        String idNumber = request.getParameter("idNumber");
        String idType = request.getParameter("idType");
        String realName = request.getParameter("realName");
        String clientNo = request.getParameter("clientNo");
        if (clientNo==null||idType==null||idNumber==null||realName==null)
        {
            return "{result:'ERROR',msg:'参数不足'}";
        }

        Map<String,Object> params = new HashMap<String, Object>();
        params.put("idNumber",idNumber);
        params.put("idType",idType);
        params.put("realName",realName);
        params.put("clientNo",clientNo);

        ServiceRequest serviceRequest = new ServiceRequest();
        serviceRequest.setParams(params);
        JSONObject resultJson = new JSONObject();
        try{
             ServiceResponse response = innerServiceUtil.call("submitTable",serviceRequest);
             String queueNumber = (String)response.getResults().get("queueNumber");
             if (queueNumber!=null)
             {
                 resultJson.put("result","ok");
                 resultJson.put("queueNumber",queueNumber);
             }
             else {
                 resultJson.put("result","error");
             }
        }catch (Exception e)
        {
            resultJson.put("result","error");
        }

        return resultJson.toJSONString();

    }


    @RequestMapping(value = "/crj/sendPaper", produces = "application/json")
    public String sendPaper(HttpServletRequest request) {


        String paperNumber = request.getParameter("paperNumber");
        String paperType = request.getParameter("paperType");
        String idNumber = request.getParameter("idNumber");
        String smsContent = request.getParameter("smsContent");
        String timestamp = request.getParameter("timestamp");
        if (paperNumber==null||paperType==null||idNumber==null)
        {
            return "{result:'ERROR',msg:'参数不足'}";
        }

        Map<String,Object> params = new HashMap<String, Object>();
        params.put("idNumber",idNumber);
        params.put("paperType",paperType);
        params.put("paperNumber",paperNumber);
        params.put("timestamp",timestamp);
        params.put("smsContent",smsContent);

        ServiceRequest serviceRequest = new ServiceRequest();
        serviceRequest.setParams(params);
        innerServiceUtil.call("sendPaper",serviceRequest);

        return "{result:'OK'}";

    }
}
